/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.users;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hutech09.jsale.bsn.IAccessUserManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.AccessGroup;
import org.hutech09.jsale.entity.AccessUser;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class GroupUserInCtrl {
//
//    @ManagedProperty("#{browseAccessUserCtrl}")
//    private BrowseAccessUserCtrl browseCtrl;

    @ManagedProperty("#{loggedInUser}")
    private LoggedInUser loggedInUser;

    @EJB
    private IAccessUserManager userManager;

    private AccessUser user;

    private GroupDataModel dataModel;

    private AccessGroup[] selectedGroups;

    public GroupUserInCtrl() {
    }

    public GroupDataModel getDataModel() {
        if (dataModel == null) {
            dataModel = new GroupDataModel(userManager.getAllGroup());
        }
        return dataModel;
    }

    public void setDataModel(GroupDataModel dataModel) {
        this.dataModel = dataModel;
    }

    public AccessGroup[] getSelectedGroups() {
        return selectedGroups;
    }

    public void setSelectedGroups(AccessGroup[] selectedGroups) {
        this.selectedGroups = selectedGroups;
        user.setAccessGroups(Arrays.asList(selectedGroups));
    }

    public String delete() {
        try {
            if (user.getId().equals(loggedInUser.getCurrentUser().getId())) {
                FacesContext.getCurrentInstance().addMessage("applyMessages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "You can't delete yourselt.", ""));
                return "";
            }
            userManager.delete(user.getId());
            FacesContext.getCurrentInstance().addMessage("applyMessages", new FacesMessage(FacesMessage.SEVERITY_INFO, "User is deleted.", ""));
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(GroupUserInCtrl.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage("applyMessages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Deleting user error.", ""));
            return "";
        }
        return "userBrowsePage";
    }

    public AccessUser getUser() {
        return user;
    }

    public void setUser(AccessUser user) {
        this.user = user;
        if (user != null) {
            selectedGroups = new AccessGroup[user.getAccessGroups().size()];
            for (int i = 0; i < user.getAccessGroups().size(); i++) {
                selectedGroups[i] = user.getAccessGroups().get(i);
            }
        }
    }

    public void update() {
        System.err.println(user);
        System.err.println(user.getAccessGroups());
        try {
            userManager.update(user);
            FacesContext.getCurrentInstance().addMessage("applyMessages", new FacesMessage(FacesMessage.SEVERITY_INFO, "Updated!", ""));
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(GroupUserInCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public LoggedInUser getLoggedInUser() {
        return loggedInUser;
    }

    public void setLoggedInUser(LoggedInUser loggedInUser) {
        this.loggedInUser = loggedInUser;
    }
}
